$(document).ready(function () {
    $("form").submit(function () {
        return false;
    });
    
    var VAL = {
        NORMAL: "0000000",
        EVT_USER : $("#userId").text(),
        T_FWPWMSOPE: "FWPWMSOPE",
        T_FDPAPLYWO: "FDPAPLYWO",
        T_FBPBISDAT: "FBPBISDAT",
        DISABLED_ATTR: {
            "disabled": true
        },
        ENABLED_ATTR: {
            "disabled": false
        }
    };
    
    var domObj = {
    	W : $(window),

        $woCateSel : $("#woCateSel"),
        $woIdSel : $("#woIdSel"),
        $plnPrdQtyTxt : $("#plnPrdQtyTxt"),
        $whInPrdQtyTxt : $("#whInPrdQtyTxt"),
        $boxIdTxt : $("#boxIdTxt"),
        $destShopSel : $("#destShopSel"),
//        $storageLocSel : $("#storageLocSel"),
        $relateUsrTxt : $("#relateUsrTxt"),

        grids: {
	        $UnderDiv      : $(".UnderDiv"),
	        $boxInfoGrdDiv : $("#boxInfoGrdDiv"),
            $boxInfoGrd    : $("#boxInfoGrd")
        },

        buttons: {
            $query_btn: $("#query_btn"),
            $register_btn: $("#register_btn"),
            $clean_btn: $("#clean_btn")
        }
    };
    
    function initBoxInfoGrd() {
        domObj.grids.$boxInfoGrd.jqGrid({
            url: "",
            datatype: "local",
            mtype: "POST",
            height: 500,
            width: $("#boxInfoGrdDiv").width() * 0.99,
            autowidth: false,
            shrinkToFit: true,
            resizable: true,
            loadonce: true,
            viewrecords: true,
            rownumbers: true,
            rowNum: 20,
            emptyrecords: true,
            multiselect: true,
            colModel: [{
                name: 'box_id',
                index: 'box_id',
                label: '箱号',
                width: 180
            }, {
                name: 'box_stat',
                index: 'box_stat',
                label: '状态',
                width: 150
            }, {
                name: 'wo_id',
                index: 'wo_id',
                label: '工单号',
                width: 180
            }, {
                name: 'mdl_id_fk',
                index: 'mdl_id_fk',
                label: '产品代码',
                width: 180
            }, {
                name: 'prd_qty',
                index: 'prd_qty',
                label: '玻璃计数',
                width: 180
            }, {
                name: 'cus_id_fk',
                index: 'cus_id_fk',
                label: '客户代码',
                width: 180
            }],

            pager: "boxInfoPg"
        });
    }

    //初始化下拉框
    var toolFnc = {
        iniWoCateSel : function(){
            comAddValueByDataCateFnc("#woCateSel","WOCT","data_ext","data_desc",true);
        },
        //1.初始化内部订单
        initWoIdFnc: function () {
            domObj.$plnPrdQtyTxt.val("");
            domObj.$whInPrdQtyTxt.val("");
            var inObj, outObj, i, tblCnt, oary, warp, option;
            var wo_cate = domObj.$woCateSel.find("option:selected").val();
            var iary = {
            	wo_cate  : wo_cate,
                wo_stat: 'RLSE'//能入库的订单都是下线的订单？
            }
            inObj = {
                trx_id: VAL.T_FDPAPLYWO,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                var oaryA = outObj.oary;
                var compare = function (obj1, obj2) {
                    var val1 = obj1.wo_id;
                    var val2 = obj2.wo_id;
                    if (val1 < val2) {
                        return -1;
                    } else if (val1 > val2) {
                        return 1;
                    } else {
                        return 0;
                    }
                }
                oaryA.sort(compare);
                SelectDom.addSelectArr(domObj.$woIdSel,oaryA, "wo_id", "wo_id", "", true);
            }
        },

        initDestShop: function () {
            comAddValueByDataCateFnc("#destShopSel", "DEST", "data_ext", "data_desc", true);
        },

//        iniStorageLocSel: function () {
//            var destShop = domObj.$destShopSel.val();
//            var inObj = {
//                trx_id: VAL.T_FBPBISDAT,
//                action_flg: 'Q',
//                iary: [{
//                    data_cate: 'STLC',
//                    data_id: destShop
//                }]
//            };
//            var outObj = comTrxSubSendPostJson(inObj);
//            if (outObj.rtn_code === VAL.NORMAL) {
//                SelectDom.addSelectArr(domObj.$storageLocSel, outObj.oary, "data_ext", "data_desc", "", true);
//            }
//        },
        prdQtyChgFnc : function() {
            var inObj, outObj, iary, tblCnt, wo_id;
            woId = domObj.$woIdSel.val();
            iary = {
                wo_id: woId
            };
            inObj = {
                trx_id: VAL.T_FDPAPLYWO,
                action_flg: "Q",
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                domObj.$plnPrdQtyTxt.val(outObj.oary[0].pln_prd_qty);
                domObj.$whInPrdQtyTxt.val(outObj.oary[0].wh_in_prd_qty);
            }
        }
    };

    var buttonFnc = {
        queryFnc: function () {
            var woIdFk, boxIdFk, iary, inObj, outObj;
            woIdFk = domObj.$woIdSel.val();
            boxIdFk = domObj.$boxIdTxt.val();
            /*
             if (!woIdFk) {
             showErrorDialog("", "内部订单不能为空,请选择");
             return false;
             }*/

            iary = {
                wo_id: woIdFk,
                box_id: boxIdFk
            };
            inObj = {
                trx_id: VAL.T_FWPWMSOPE,
                action_flg: 'Q',
                iary: [iary]
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                setGridInfo(outObj.oary, "#boxInfoGrd", true);
            }
        },

        registerFnc: function () {
            var destShop, relateUsr, storageLoc, ids, inObj, outObj, len;
            destShop = domObj.$destShopSel.val();
            relateUsr = domObj.$relateUsrTxt.val();
//            storageLoc = domObj.$storageLocSel.val();

            //选中的箱子
            ids = domObj.grids.$boxInfoGrd.jqGrid('getGridParam', 'selarrrow');

            if (ids.length === 0) {
                showErrorDialog("", "请选择需要入库的箱子！");
                return false;
            }

            if (!destShop) {
                showErrorDialog("", "仓位不能为空,请选择！");
                return false;
            }
//            if (!storageLoc) {
//                showErrorDialog("", "储位不能为空,请选择");
//                return false;
//            }
            if (!relateUsr) {
                showErrorDialog("", "送货人不能为空,请输入！");
                return false;
            }

            var box = [];
            for (var i = 0; i < ids.length; i++) {
                rowData = domObj.grids.$boxInfoGrd.jqGrid("getRowData", ids[i]);
                box.push(rowData);
            }

            inObj = {
                trx_id: VAL.T_FWPWMSOPE,
                action_flg: 'I',
                iary: box,
                evt_user: VAL.EVT_USER,
                dest_shop: destShop,
                storage_loc: storageLoc,
                relate_usr: relateUsr
            };
            outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                len = ids.length;
                for (var i = 0; i < len; i++) {
                    domObj.grids.$boxInfoGrd.jqGrid("delRowData", ids[0]);
                }
                showSuccessDialog("入库成功！");
            }
        },

        cleanFnc: function () {
        	SelectDom.initWithSpace(domObj.$woCateSel);
        	SelectDom.initWithSpace(domObj.$woIdSel);
        	SelectDom.initWithSpace(domObj.$destShopSel);
//            SelectDom.initWithSpace(domObj.$storageLoc);
            domObj.$plnPrdQtyTxt.val("");
            domObj.$whInPrdQtyTxt.val("");
            domObj.$boxIdTxt.val("");
            domObj.$relateUsrTxt.val("");
            domObj.grids.$boxInfoGrd.jqGrid('clearGridData');
            toolFnc.iniWoCateSel();
            toolFnc.initWoIdFnc();
            toolFnc.initDestShop();
        }

    };
  
    var iniContorlData = function(){
        domObj.$woCateSel.change(function(){
        	toolFnc.initWoIdFnc();
        });
        
        domObj.$woIdSel.change(function(){
        	toolFnc.prdQtyChgFnc();
        });
        
//        domObj.$destShopSel.change(function () {
//            toolFnc.iniStorageLocSel();
//        });
    };
    
    var iniButtonAction = function() {
        domObj.buttons.$query_btn.bind('click', buttonFnc.queryFnc);
        domObj.buttons.$register_btn.bind('click', buttonFnc.registerFnc);
        domObj.buttons.$clean_btn.bind('click', buttonFnc.cleanFnc);
        
        domObj.$boxIdTxt.keydown(function (event) {
            var inObj, outObj, tblCnt, boxId, newRowId, iary;
            boxId = domObj.$boxIdTxt.val();
            iary = {
                box_id: boxId
            };
            if (event.which == 13) {
                if (boxId == "") {
                    showErrorDialog("", "请输入箱号!");
                    return false;
                }
                var rowDatas = domObj.grids.$boxInfoGrd.getRowData();
                for (var i = 0; i < rowDatas.length; i++) {
                    if (rowDatas[i].box_id == boxId) {
                        showErrorDialog("", "箱号已存在，请重新输入！");
                        return false;
                    }
                }

                inObj = {
                    trx_id: VAL.T_FWPWMSOPE,
                    action_flg: 'Q',
                    iary: [iary]
                }
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    newRowId = getGridNewRowID("#boxInfoGrd");
                    domObj.grids.$boxInfoGrd.jqGrid("addRowData", newRowId, outObj.oary[0]);
                }
            }
        });
	}
    
    var initializationFunc = function(){
    	initBoxInfoGrd();
        iniContorlData();
        iniButtonAction();
    	buttonFnc.cleanFnc();
    };
    initializationFunc();
    
    //模态框拖动
    $(".modal-content").draggable();
    //表格自适应
    function resizeFnc(){
	    var grdDivHeight,grdDivWidth;
	    grdDivWidth = domObj.W.width() - 60;
	    grdDivHeight = domObj.W.height() - domObj.grids.$UnderDiv.offset().top;
	
	    domObj.grids.$UnderDiv.height(grdDivHeight);
	    domObj.grids.$boxInfoGrdDiv.width(grdDivWidth);
	    domObj.grids.$boxInfoGrdDiv.height(grdDivHeight*0.99 - 51);
	    domObj.grids.$boxInfoGrd.setGridWidth(grdDivWidth*0.99);
	    domObj.grids.$boxInfoGrd.setGridHeight(grdDivHeight*0.95 - 51 - 35);
    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });

});
